package com.achang.achangmall.product.service.impl;

import com.achang.achangmall.product.entity.AttrAttrgroupRelationEntity;
import com.achang.achangmall.product.entity.AttrGroupEntity;
import com.achang.achangmall.product.entity.CategoryEntity;
import com.achang.achangmall.product.service.AttrAttrgroupRelationService;
import com.achang.achangmall.product.service.AttrGroupService;
import com.achang.achangmall.product.service.CategoryService;
import com.achang.achangmall.product.vo.AttrGroupRelationVo;
import com.achang.achangmall.product.vo.AttrRespVo;
import com.achang.achangmall.product.vo.AttrVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.achang.common.utils.PageUtils;
import com.achang.common.utils.Query;

import com.achang.achangmall.product.dao.AttrDao;
import com.achang.achangmall.product.entity.AttrEntity;
import com.achang.achangmall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
@Transactional
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttr(AttrVo vo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(vo,attrEntity);
        this.save(attrEntity);

        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(vo.getAttrGroupId());
        relationEntity.setAttrId(vo.getAttrId());
        attrAttrgroupRelationService.save(relationEntity);
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Integer catelogId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (catelogId !=0){
            wrapper.eq("catelog_id",catelogId);
        }

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("catelog_id",key).or().like("attr_name",key);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> list = page.getRecords();
        List<AttrRespVo> resultList = list.stream().map(item -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(item, attrRespVo);
            AttrAttrgroupRelationEntity attrId = attrAttrgroupRelationService.
                    getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().
                            eq("attr_id", item.getAttrId()));
            if (attrId != null) {
                AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrId);
                attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }

            CategoryEntity categoryEntity = categoryService.getById(item.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(resultList);

        return pageUtils;
    }

    //根据分组id，找到对应关联属性
    @Override
    public List<AttrEntity> getRelationAttr(Integer attrgroupId) {
        List<AttrAttrgroupRelationEntity> entityList = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> attrIdList = entityList.stream().map(e -> {
            return e.getAttrId();
        }).collect(Collectors.toList());

        if (attrIdList == null && attrIdList.size() == 0){
            return null;
        }

        List<AttrEntity> attrEntityList = this.listByIds(attrIdList);
        return attrEntityList;
    }

    @Override
    public void deleteRelation(List<AttrGroupRelationVo> voList) {
        voList.forEach(item ->{
            attrAttrgroupRelationService.
                    remove(new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_id",item.getAttrId())
                            .eq("attr_group_id",item.getAttrGroupId()));
        });

    }

    @Override
    public PageUtils getNotRelationAttr(Integer attrgroupId, Map<String, Object> params) {

        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        List<AttrGroupEntity> groupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));

        List<Long> list = groupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());

        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.
                list(new QueryWrapper<AttrAttrgroupRelationEntity>().
                        in("attr_group_id", list));

        List<Long> attrIdList = relationEntities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
                .eq("catelog_id", catelogId);

        if (attrIdList!= null && attrIdList.size()>0){
            wrapper.notIn("attr_id", attrIdList);
        }

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("attr_id",key).or().like("attr_name",key);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //在指定的所有属性集合中，筛选中检索属性
    @Override
    public List<Long> selectSearchAttrIds(List<Long> attrIdList) {

        List<AttrEntity> attrEntities = this.baseMapper.selectList(new QueryWrapper<AttrEntity>()
                .eq("search_type", 1)
                .in("attr_id", attrIdList).select("attr_id"));

        List<Long> searchAttrIdList = attrEntities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());

        return searchAttrIdList;
    }

}